package org.biz.news.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.biz.impl.BaseBiz;
import org.biz.news.IHomeBiz;
import org.hibernate.dao.IBaseDao;
import org.hibernate.entity.Column;
import org.hibernate.entity.Friends;
import org.hibernate.entity.Links;
import org.hibernate.entity.NewsContents;
import org.hibernate.entity.NewsTitle;
import org.hibernate.entity.Suggestion;

/**
 * 
 * 前台业务类对象
 * 
 * @Title: HomeBiz.java
 * @Description:新闻系统(一期)
 * @Copyright: Copyright (c) 2011
 * @Company:北京学邦时代
 * @author 苗有虎
 * @date Oct 23, 2012 10:56:56 AM
 * @version V4.0
 */
@SuppressWarnings("unchecked")
public class HomeBiz extends BaseBiz implements IHomeBiz {
	private IBaseDao homeDao = null; // 业务类数据层对象

	public IBaseDao getHomeDao() {
		return homeDao;
	}

	public void setHomeDao(IBaseDao homeDao) {
		this.homeDao = homeDao;
	}

	/**
	 * 友情链接
	 * @param 1:省厅，2：大学，3：企业
	 */

	public List<Friends> selFriends(int ifusing) {
		return this.homeDao.getObjectBySQL(Friends.class,"select * from friends where ifusing="+ifusing+" order by ifindex,id");
	}

	/**
	 * 查询网站的最新文章;
	 */
	public List<NewsTitle> selNewsTitles(int pageSize) {
		return this.homeDao.getObjectBySQL(NewsTitle.class,
				"select * from news_title where red=0 order by id desc limit " + pageSize);
	}

	/**
	 * 关注频道一条带图片的推荐文章;
	 */
	public List<NewsTitle> selNewsTJByCid(String cid) {
		return this.homeDao
				.getObjectBySQL(
						NewsTitle.class,
						"select * from news_title where red=0 and ifelite=1 and cid in(" + cid + ") order by posttime desc limit 5");
	}

	/**
	 * 关注频道的最新的置顶文章;
	 */
	public List<NewsTitle> selNewsTopByCid(int cid, int pageSize) {
		return this.homeDao
				.getObjectBySQL(
						NewsTitle.class,
						"select * from news_title where red=0 and iftop=1 and cid=1 order by posttime desc limit "
								+ pageSize);
	}

	/**
	 * 本栏目含有图片的一条新闻;
	 */
	public NewsTitle selColumnPic(int cid) {

		return (NewsTitle) this.homeDao.loadByHql("from NewsTitle where red=0 and cid="
				+ cid + " and ispic=1 order by posttime desc limit 1");
	}

	/**	
	 * 本栏目下的最新新闻;
	 */
	public List<NewsTitle> selColumnNews(int cid, int pageSize) {
		return this.homeDao.getObjectBySQL(NewsTitle.class,
				"select * from news_title where red=0 and cid=" + cid
						+ " order by posttime desc limit " + pageSize);
	}

	
	/** 20131223 add by xy start... */
	/**
	 * 推荐文章
	 */
	public List<NewsTitle> searchByRecommend(int limit){
		
		// 推荐，不在回收站中，普通新闻
		String strHql = "from NewsTitle n where n.ifelite!=0 and n.red=0 and n.newType=0 " +
				"order by n.ispic desc,n.ifelite desc,n.posttime desc";
		if(limit == 0){
			return homeDao.query(strHql);
		} else {
			return homeDao.query(strHql,1,limit);
		}
	}

	/**
	 * 咨询排行
	 */
	public List<NewsTitle> searchConsultingOrder(int intDays,int limit){
		
		try{
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DAY_OF_WEEK, intDays);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String strDate = sdf.format(calendar.getTime());
			String strHql = "from NewsTitle n where n.red=0 and n.newType=0 and n.posttime >='" + strDate + "' order by n.pv desc,n.posttime desc,n.id desc";
			System.out.println("++++++++++++++++++++++++");
			System.out.println(strHql);
			System.out.println("++++++++++++++++++++++++");
			if(limit==0){
				return homeDao.query(strHql);
			}else{
				return homeDao.query(strHql,1,limit);
			}
		} catch(Exception e){
			return null;
		}
	}
	
	/**
	 * 通过NewsType查找新闻
	 * @param newsType
	 * @return
	 */
	public List<NewsTitle> searchNewsByType(Integer newsType,Integer limit) {
		
		StringBuilder sbHql = new StringBuilder("from NewsTitle n where n.red=0 and n.newType = ");
		sbHql.append(newsType);
		sbHql.append(" order by n.iftop desc,n.posttime desc,n.id desc");
		if(limit == 0){
			return homeDao.query(sbHql.toString());
		} else {
			return homeDao.query(sbHql.toString(),1,limit);
		}
	}
	
	/**
	 * 查找重要模块
	 */
	public List<Column> searchImportParts(){
		
		String strHql = "from Column c where c.menuType!=0 and c.menuColumns=null order by c.menuType,c.sortWay,c.id";
		return homeDao.query(strHql);
	}
	
	/**
	 * 通过查看栏目的静态名称的方式查看新闻数据
	 */
	public List<NewsTitle> selNewsByColumn(String strCids, int pageNo,
			int pageSize,boolean hasNotPic) {

		String strHql = "select * from news_title where red=0 and cid in(" + strCids + ")";
		System.out.println("cids:" + strCids);
		if(hasNotPic){
			strHql = strHql + " order by iftop desc,posttime desc,id desc ";
		} else {
			strHql = strHql + " and TRIM(picurl)!='' order by ispic desc,iftop desc,posttime desc,id desc ";
		}
		// 目前不考虑置顶
		List<NewsTitle> nts = this.homeDao.getPageBySql(NewsTitle.class,strHql
				, pageNo, pageSize);
		
		return nts;
	}
	
	/**
	 * 通过查看栏目的静态名称的方式查看新闻数据
	 */
	public List<NewsTitle> selNewsByColumn(String strCids, int pageNo,
			int pageSize){
		return selNewsByColumn(strCids,pageNo,pageSize,true);
	}
	
	/**
	 * 通过查看栏目的静态名称的方式查看新闻数据
	 */
	public List<NewsTitle> selNewsByColumn(String strCids, int pageNo,
			int pageSize,boolean hasNotPic,Integer intRemoveID) {

		String strHql = "select * from news_title where red=0 and id!="+ intRemoveID + " and cid in(" + strCids + ")";
		if(hasNotPic){
			strHql = strHql + " order by iftop desc,posttime desc,id desc ";
		} else {
			strHql = strHql + " order by ispic desc,iftop desc,posttime desc,id desc ";
		}
		// 目前不考虑置顶
		List<NewsTitle> nts = this.homeDao.getPageBySql(NewsTitle.class,strHql
				, pageNo, pageSize);
		
		return nts;
	}
	
	/**
	 * 获得其子栏目
	 * 
	 * @param columns 父栏目
	 * @return
	 */
	public List<Column> listChildColumns(Column columns) {
		if (columns == null) {
			return null;
		} else {
			return this.homeDao.query("from Column as a where a.menuColumns.id="
					+ columns.getId() + " order by a.sortWay,a.id");
		}
	}
	
	
	/**
	 * 最新文章
	 */
	public List<NewsTitle> listNewestArciles(int limit){

		//String strHql = "from NewsTitle nt where nt.red=0 order by nt.posttime desc,nt.id desc";
		String strHql = "from NewsTitle nt where nt.cid NOT in (23,54,55) AND nt.red=0 order by nt.posttime desc,nt.id desc";
		return homeDao.query(strHql, 1, limit);
	}
	/** 20131223 add by xy end... */
	
	/** 20140223 add by xy start..*/
	/**
	 * 查找幻灯片
	 */
	public List<Links> searchSlides(){
		
		return searchLinksByType(1);
	}
	
	/**
	 * 查找公告
	 */
	public List<Links> searchAds(){
		
		return searchLinksByType(2);
	}
	
	/**
	 * 通过类型查找
	 */
	private List<Links> searchLinksByType(int intType){

		String strHql = "from Links where type="+intType+" order by sort desc,id desc";
		return homeDao.query(strHql);
	}

	/**
	 * 获取栏目标题
	 * @param strCids
	 * @param pageNo
	 * @param pageSize
	 * @param hasNotPic
	 * @param intRemoveID
	 * @return
	 */
	@SuppressWarnings("unused")
	public List<NewsTitle>  findPartNews(String strCids, int pageNo,
			int pageSize,boolean hasNotPic,Integer intRemoveID){
			String strHql = "select * from news_title where red=0 and id!="+ intRemoveID + " and cid in(" + strCids + ")";
			if(hasNotPic){
				strHql = strHql + " order by iftop desc,posttime desc,id desc ";
			} else {
				strHql = strHql + " order by ispic desc,iftop desc,posttime desc,id desc ";
			}
			// 目前不考虑置顶
			List<NewsTitle> nts = this.homeDao.getPageBySql(NewsTitle.class,strHql
					, pageNo, pageSize);
			
			return nts;
	}
	// 获取栏目对象
	public Column findColumnObject(Integer cid) {
		return (Column) this.homeDao.loadByHql("from Column where id = " + cid);
	}
	
	public List<NewsTitle>  findPartNews(String strCids, int pageNo,
			int pageSize,boolean hasNotPic){
	String strHql = "select * from news_title where red=0 and cid in(" + strCids + ")";
	System.out.println("cids:" + strCids);
	if(hasNotPic){
		strHql = strHql + " order by iftop desc,posttime desc,id desc ";
	} else {
		strHql = strHql + " and TRIM(picurl)!='' order by ispic desc,iftop desc,posttime desc,id desc ";
	}
	// 目前不考虑置顶
	List<NewsTitle> nts = this.homeDao.getPageBySql(NewsTitle.class,strHql
			, pageNo, pageSize);
	   return nts;
	}
	
	/**
	 * 问答
	 */
	public List<Suggestion>  findSuggestion( int pageNo,
			int pageSize,boolean hasNotPic){
	String strHql = "select * from suggestion where sign=1 ";
	if(hasNotPic){
		strHql = strHql + " order by  create_date desc,id desc ";
	} 
	// 目前不考虑置顶
	List<Suggestion> nts = this.homeDao.getPageBySql(Suggestion.class,strHql
			, pageNo, pageSize);
	   return nts;
	}
	
	/**
	 * 文章内容查询
	 */
	public List<NewsContents> findContentsByTid(int tid, int pageNo,
			int pageSize){
		return this.homeDao.getPageBySql(NewsContents.class,
				"select id , tid , cid ,uid, contents,posttime from news_contents where tid="
						+ tid + " order by id", pageNo, pageSize);
	}
	
	public List<Column> listChildColumns() {
		
		return this.homeDao.query("from Column as a  order by a.sortWay,a.id");
	}
	@Override
	public List<NewsTitle> selBySearch(String searcheText, int pageNo, int pageSize) {
		return this.homeDao.getPageBySql(NewsTitle.class,
				"select * from news_title where red=0 and TRIM(title) like '%" + searcheText + "%' order by iftop desc,posttime desc,id desc", pageNo, pageSize);
	}
	@Override
	public int doCountSearch(String searcheText) {
		return this.homeDao
				.countSqlQuery("select count(*) from news_title where red=0 and TRIM(title) like '%" + searcheText + "%' order by iftop desc,posttime desc,id desc");
	}
}
